﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Gmantis.Controls
{
    public class CustomConverter : IValueConverter
    {
        // Fields
        private Func<object, Type, object, CultureInfo, object> _converterBackFunction;
        private Func<object, Type, object, CultureInfo, object> _converterFunction;

        // Methods
        private CustomConverter(Func<object, Type, object, CultureInfo, object> converterFunction, Func<object, Type, object, CultureInfo, object> converterBackFunction)
        {
            this._converterFunction = converterFunction;
            this._converterBackFunction = converterBackFunction;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (this._converterFunction != null)
            {
                return this._converterFunction(value, targetType, parameter, culture);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (this._converterBackFunction != null)
            {
                return this._converterBackFunction(value, targetType, parameter, culture);
            }
            return null;
        }

        public static IValueConverter Create(Func<object, Type, object, CultureInfo, object> converterFunction)
        {
            return new CustomConverter(converterFunction, converterFunction);
        }

        public static IValueConverter Create(Func<object, Type, object, CultureInfo, object> converterFunction, Func<object, Type, object, CultureInfo, object> converterBackFunction)
        {
            return new CustomConverter(converterFunction, converterBackFunction);
        }
    }
}
